package org.easy.control.controller;

import lombok.extern.slf4j.Slf4j;
import org.easy.common.restinfo.RestInfo;
import org.easy.common.vo.SysDictVO;
import org.easy.module.service.SysDictDataService;
import org.easy.module.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: DictController
 * @Description: 数据字典Controller
 * @Author zhouzhixin
 * @Date 2021/10/9
 * @Version 1.0
 */
@Slf4j
@RequestMapping("/dict")
@RestController
public class SysDictController {

    SysDictService sysDictService;

    SysDictDataService sysDictDataService;

    @Autowired
    public void init(SysDictService sysDictService, SysDictDataService sysDictDataService) {
        this.sysDictService = sysDictService;
        this.sysDictDataService = sysDictDataService;
    }

    /**
     * <p>JSON列表查询</p>
     *
     * @return java.lang.String
     * @author zhouzhixin8
     * @date 2021/10/9 11:11
     */
    @GetMapping("/queryJson")
    public RestInfo<List<SysDictVO>> queryJson() {
        log.info("数据字典列表查询---start");
        List<SysDictVO> sysDictVOList = null;
        try {
            sysDictVOList = sysDictService.queryList();
        } catch (Exception e) {
            log.error("数据字典列表查询过程出现异常", e);
        } finally {
            log.info("数据字典列表查询---end");
        }
        return RestInfo.ok(sysDictVOList);
    }

    /**
     * <p>根据id查询数据字典</p>
     *
     * @return java.lang.String
     * @author zhouzhixin8
     * @date 2021/10/9 11:11
     */
    @GetMapping("/queryById")
    public RestInfo<SysDictVO> queryById(@RequestParam(required = false) String id) {
        return sysDictService.queryById(id);
    }

    /**
     * <p>新增</p>
     *
     * @param sysDictVO
     * @return java.lang.String
     * @author zhouzhixin
     * @date 2021/10/9 13:33
     */
    @PostMapping("/add")
    public String add(SysDictVO sysDictVO, Model model) {
        log.info("数据字典新增---start");
        try {
            sysDictService.add(sysDictVO, model);
        } catch (Exception e) {
            log.error("数据字典新增过程出现异常", e);
        } finally {
            log.info("数据字典新增---end");
        }
        return "dict/dict";
    }

    /**
     * <p>修改</p>
     *
     * @param sysDictVO
     * @return java.lang.String
     * @author zhouzhixin
     * @date 2021/10/11 10:15
     */
    @PutMapping("/update")
    public RestInfo<Void> update(@RequestBody SysDictVO sysDictVO) {
        log.info("数据字典修改---start");
        try {
            return sysDictService.update(sysDictVO);
        } catch (Exception e) {
            log.error("数据字典修改过程出现异常", e);
        } finally {
            log.info("数据字典修改---end");
        }
        return RestInfo.fail().build();
    }

    /**
     * <p>删除</p>
     *
     * @param id
     * @return java.lang.String
     * @author zhouzhixin
     * @date 2021/10/9 14:21
     */
    @DeleteMapping("/delete/{id}")
    public RestInfo<Void> delete(@PathVariable String id) {
        log.info("数据字典删除---start");
        try {
            return sysDictService.delete(id);
        } catch (Exception e) {
            log.error("数据字典删除过程出现异常", e);
        } finally {
            log.info("数据字典删除---end");
        }
        return RestInfo.fail().build();
    }

}
